package com.esms.controller;

import com.esms.dao.AppraisalAssResMapper;
import com.esms.po.AppraisalAssRes;
import com.esms.vo.AppraisalAssResPages;
import com.esms.vo.AppraisalAssResVo;
import com.esms.vo.EmployeeAttendanceVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


/**
 * 评议结果信息
 */
@Controller
@RequestMapping("/AppraisalAss")
public class AppraisalAssResController {

    @Autowired
    public AppraisalAssResMapper appraisalAssResMapper;

    /**
     * 下级对上级民主评议考核结果
     * @return
     */
    @RequestMapping(value = "L2HDemocratic.do")
    @ResponseBody
    public AppraisalAssResPages L2HDemocraticAppraisalAssRes(
            @RequestParam(value="page", defaultValue="1")int pageNum,
            @RequestParam(value="limit", defaultValue="5") int limit,
            @RequestParam(value = "download",required = false,defaultValue = "false") Boolean download,
            HttpServletResponse response

    ) throws IOException {
        List<AppraisalAssResVo> resList = new ArrayList<>();
        try {
            PageHelper.startPage(pageNum, limit);
            resList = appraisalAssResMapper.listL2H();
        }catch (Exception e){
            e.printStackTrace();
        }
        PageInfo pageResult = new PageInfo(resList);
        AppraisalAssResPages resPages = new AppraisalAssResPages();
        resPages.setData(pageResult.getList());
        resPages.setCode(0);
        resPages.setCount((int) pageResult.getTotal());
        resPages.setMsg("");
        if (download){
            //创建excel文件
            HSSFWorkbook wb = new HSSFWorkbook();
            //创建sheet页
            HSSFSheet sheet = wb.createSheet("下级对上级民主评议信息统计");
            //创建标题行
            HSSFRow titleRow = sheet.createRow(0);
            titleRow.createCell(0).setCellValue("部门号");
            titleRow.createCell(1).setCellValue("部门名称");
            titleRow.createCell(2).setCellValue("评价员工号");
            titleRow.createCell(3).setCellValue("评价人姓名");
            titleRow.createCell(4).setCellValue("被评价员工号");
            titleRow.createCell(5).setCellValue("被评价员工姓名");
            titleRow.createCell(6).setCellValue("状态");
            titleRow.createCell(7).setCellValue("编制类型");

            for (AppraisalAssResVo item :
                    resList) {
                HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
                dataRow.createCell(0).setCellValue(item.getdId());
                dataRow.createCell(1).setCellValue(item.getdName());
                dataRow.createCell(2).setCellValue(item.geteAccount());
                dataRow.createCell(3).setCellValue(item.geteName());
                dataRow.createCell(4).setCellValue(item.getRatedAccount());
                dataRow.createCell(5).setCellValue(item.getRatedEName());
                dataRow.createCell(6).setCellValue(item.getStatus());
                dataRow.createCell(7).setCellValue(item.getType());
            }



            // 设置下载时客户端Excel的名称   （上面注释的改进版本，上面的中文不支持）
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    + new String("周纪实分数统计".getBytes(), "iso-8859-1") + ".xls");
            OutputStream ouputStream = response.getOutputStream();
            wb.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
        }
        return resPages;
    }

    /**
     * 上级对下级民主评议考核结果
     * @return
     */
    @RequestMapping(value = "H2LDemocratic.do")
    @ResponseBody
    public AppraisalAssResPages H2LDemocraticAppraisalAssRes(
            @RequestParam(value="page", defaultValue="1")int pageNum,
            @RequestParam(value="limit", defaultValue="5") int limit,
            @RequestParam(value = "download",required = false,defaultValue = "false") Boolean download,
            HttpServletResponse response

    ) throws IOException {
        List<AppraisalAssResVo> resList = new ArrayList<>();
        try {
            PageHelper.startPage(pageNum, limit);
            resList = appraisalAssResMapper.listH2L();
        }catch (Exception e){
            e.printStackTrace();
        }
        PageInfo pageResult = new PageInfo(resList);
        AppraisalAssResPages resPages = new AppraisalAssResPages();
        resPages.setData(pageResult.getList());
        resPages.setCode(0);
        resPages.setCount((int) pageResult.getTotal());
        resPages.setMsg("");
        if (download){
            //创建excel文件
            HSSFWorkbook wb = new HSSFWorkbook();
            //创建sheet页
            HSSFSheet sheet = wb.createSheet("下级对上级民主评议信息统计");
            //创建标题行
            HSSFRow titleRow = sheet.createRow(0);
            titleRow.createCell(0).setCellValue("部门号");
            titleRow.createCell(1).setCellValue("部门名称");
            titleRow.createCell(2).setCellValue("评价员工号");
            titleRow.createCell(3).setCellValue("评价人姓名");
            titleRow.createCell(4).setCellValue("被评价员工号");
            titleRow.createCell(5).setCellValue("被评价员工姓名");
            titleRow.createCell(6).setCellValue("状态");
            titleRow.createCell(7).setCellValue("编制类型");

            for (AppraisalAssResVo item :
                    resList) {
                HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
                dataRow.createCell(0).setCellValue(item.getdId());
                dataRow.createCell(1).setCellValue(item.getdName());
                dataRow.createCell(2).setCellValue(item.geteAccount());
                dataRow.createCell(3).setCellValue(item.geteName());
                dataRow.createCell(4).setCellValue(item.getRatedAccount());
                dataRow.createCell(5).setCellValue(item.getRatedEName());
                dataRow.createCell(6).setCellValue(item.getStatus());
                dataRow.createCell(7).setCellValue(item.getType());
            }



            // 设置下载时客户端Excel的名称   （上面注释的改进版本，上面的中文不支持）
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    + new String("周纪实分数统计".getBytes(), "iso-8859-1") + ".xls");
            OutputStream ouputStream = response.getOutputStream();
            wb.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
        }
        return resPages;
    }


    /**
     * 同级民主评议考核结果
     * @return
     */
    @RequestMapping(value = "EqDemocratic.do")
    @ResponseBody
    public AppraisalAssResPages EqDemocraticAppraisalAssRes(
            @RequestParam(value="page", defaultValue="1")int pageNum,
            @RequestParam(value="limit", defaultValue="5") int limit,
            @RequestParam(value = "download",required = false,defaultValue = "false") Boolean download,
            HttpServletResponse response

    ) throws IOException {
        List<AppraisalAssResVo> resList = new ArrayList<>();
        try {
            PageHelper.startPage(pageNum, limit);
            resList = appraisalAssResMapper.listEq();
        }catch (Exception e){
            e.printStackTrace();
        }
        PageInfo pageResult = new PageInfo(resList);
        AppraisalAssResPages resPages = new AppraisalAssResPages();
        resPages.setData(pageResult.getList());
        resPages.setCode(0);
        resPages.setCount((int) pageResult.getTotal());
        resPages.setMsg("");
        if (download){
            //创建excel文件
            HSSFWorkbook wb = new HSSFWorkbook();
            //创建sheet页
            HSSFSheet sheet = wb.createSheet("下级对上级民主评议信息统计");
            //创建标题行
            HSSFRow titleRow = sheet.createRow(0);
            titleRow.createCell(0).setCellValue("部门号");
            titleRow.createCell(1).setCellValue("部门名称");
            titleRow.createCell(2).setCellValue("评价员工号");
            titleRow.createCell(3).setCellValue("评价人姓名");
            titleRow.createCell(4).setCellValue("被评价员工号");
            titleRow.createCell(5).setCellValue("被评价员工姓名");
            titleRow.createCell(6).setCellValue("状态");
            titleRow.createCell(7).setCellValue("编制类型");

            for (AppraisalAssResVo item :
                    resList) {
                HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
                dataRow.createCell(0).setCellValue(item.getdId());
                dataRow.createCell(1).setCellValue(item.getdName());
                dataRow.createCell(2).setCellValue(item.geteAccount());
                dataRow.createCell(3).setCellValue(item.geteName());
                dataRow.createCell(4).setCellValue(item.getRatedAccount());
                dataRow.createCell(5).setCellValue(item.getRatedEName());
                dataRow.createCell(6).setCellValue(item.getStatus());
                dataRow.createCell(7).setCellValue(item.getType());
            }



            // 设置下载时客户端Excel的名称   （上面注释的改进版本，上面的中文不支持）
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    + new String("周纪实分数统计".getBytes(), "iso-8859-1") + ".xls");
            OutputStream ouputStream = response.getOutputStream();
            wb.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
        }
        return resPages;
    }
}
